package com.yang.servlet;

import com.yang.pojo.BasicInfo;
import com.yang.pojo.Brand;
import com.yang.service.BasicInfoService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/BasicInfoDeleteMultiServlet")
public class BasicInfoDeleteMultiServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BasicInfoService basicInfoService = new BasicInfoService();
        String[] basicInfoIds = request.getParameterValues("basicInfoIds");
        ArrayList<Integer> fails = new ArrayList<>();
        for (int i = 0; i < basicInfoIds.length; i++) {
            int basicInfoId = Integer.parseInt(basicInfoIds[i]);
            try {
                boolean b = basicInfoService.deleteBasicInfoById(basicInfoId);
                if(b == false){
                    fails.add(basicInfoId);
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        //提示信息
        String ids = "";
        for (Integer id : fails) {
            ids = ids + "," + id;
        }
        String tips = fails.size() == 0 ?"<label style='color:green'>多个评估类目删除成功</label>":ids+"<label style='color:green'>多个评估类目删除失败</label>";
        request.setAttribute("tips",tips);
        try {
            ArrayList<BasicInfo> basicInfo = basicInfoService.findBasicInfo();
            request.setAttribute("basicInfo",basicInfo);
            request.getRequestDispatcher("basic_info_list.jsp").forward(request,response);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
